home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / util / libs / type1beta5.lha / type1 / src / hints.c < prev    next >
C/C++ Source or Header  |  1994-12-17  |  27KB  |  961 lines

  1. /* $XConsortium: hints.c,v 1.4 91/10/10 11:18:13 rws Exp $ */
  2. /* Copyright International Business Machines, Corp. 1991
  3.  * All Rights Reserved
  4.  * Copyright Lexmark International, Inc. 1991
  5.  * All Rights Reserved
  6.  *
  7.  * License to use, copy, modify, and distribute this software and its
  8.  * documentation for any purpose and without fee is hereby granted,
  9.  * provided that the above copyright notice appear in all copies and that
  10.  * both that copyright notice and this permission notice appear in
  11.  * supporting documentation, and that the name of IBM or Lexmark not be
  12.  * used in advertising or publicity pertaining to distribution of the
  13.  * software without specific, written prior permission.
  14.  *
  15.  * IBM AND LEXMARK PROVIDE THIS SOFTWARE "AS IS", WITHOUT ANY WARRANTIES OF
  16.  * ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO ANY
  17.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
  18.  * AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.  THE ENTIRE RISK AS TO THE
  19.  * QUALITY AND PERFORMANCE OF THE SOFTWARE, INCLUDING ANY DUTY TO SUPPORT
  20.  * OR MAINTAIN, BELONGS TO THE LICENSEE.  SHOULD ANY PORTION OF THE
  21.  * SOFTWARE PROVE DEFECTIVE, THE LICENSEE (NOT IBM OR LEXMARK) ASSUMES THE
  22.  * ENTIRE COST OF ALL SERVICING, REPAIR AND CORRECTION.  IN NO EVENT SHALL
  23.  * IBM OR LEXMARK BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  24.  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  25.  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  26.  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
  27.  * THIS SOFTWARE.
  28.  */
  29.  
  30. // NOTE: has 1 global variable:
  31. // oldHint[MAXLABEL]
  32.  
  33.  
  34.  
  35. /*
  36.  * HINTS Module - Processing Rasterization Hints
  37.  *
  38.  * &author. Sten F. Andler; continuity by Jeffrey B. Lotspiech (lotspiech@almaden.ibm.com)
  39.  * and Duaine W. Pryor, Jr.
  40.  *
  41.  */
  42.  
  43. /*
  44.  * Include Files
  45.  *
  46.  * The included files are:
  47.  */
  48.  
  49. #ifndef T1GST
  50. #include "global.h"
  51. #endif
  52.  
  53. /*
  54.  * Global variable
  55.  */
  56. extern struct oldhintstruct oldHint[MAXLABEL];
  57.  
  58.  
  59. static void ComputeHint(struct hintsegment *hP, fractpel currX, fractpel currY, struct fractpoint *hintP);
  60. static pel SearchXofY(struct edgelist *edge, pel y);
  61. static int ImpliedHorizontalLine(struct edgelist *e1, struct edgelist *e2, int y);
  62. static void FixSubPaths(struct region *R);
  63. static void DumpSubPaths(struct edgelist *anchor);
  64. static struct edgelist *before(struct edgelist *e);
  65. static void writeXofY(struct edgelist *e, int y, int x);
  66. static void CollapseWhiteRun(struct edgelist *anchor, pel yblack, struct edgelist *left, struct edgelist *right, pel ywhite);
  67.  
  68.  
  69. /*
  70.  * InitHints() - Initialize hint data structure
  71.  */
  72.  
  73. #define ODD(x) (((int)(x)) & 01)
  74. #define FPFLOOR(fp) TOFRACTPEL((fp) >> FRACTBITS)
  75. #define FPROUND(fp) FPFLOOR((fp) + FPHALF)
  76.  
  77. void InitHints(void)
  78. {
  79.     int i;
  80.  
  81.     for (i = 0; i < MAXLABEL; i++)
  82.     {
  83.         oldHint[i].inuse = FALSE;
  84.         oldHint[i].computed = FALSE;
  85.     }
  86. }
  87.  
  88.  
  89. /*
  90.  * CloseHints(hintP) - Reverse hints that are still open
  91.  */
  92. void CloseHints(struct fractpoint *hintP)
  93. {
  94.     int i;
  95.  
  96.     for (i = 0; i < MAXLABEL; i++)
  97.     {
  98.         if (oldHint[i].inuse)
  99.         {
  100.             hintP->x -= oldHint[i].hint.x;
  101.             hintP->y -= oldHint[i].hint.y;
  102.  
  103.             oldHint[i].inuse = FALSE;
  104.  
  105.             IfTrace3((HintDebug > 1), "  Hint %d was open, hint=(%p,%p)\n",
  106.                  i, hintP->x, hintP->y);
  107.         }
  108.     }
  109. }
  110.  
  111.  
  112. /*
  113.  * ComputeHint(hP, currX, currY, hintP) - Compute the value of a hint
  114.  */
  115. static void ComputeHint(struct hintsegment *hP, fractpel currX, fractpel currY, struct fractpoint *hintP)
  116. {
  117.     fractpel currRef, currWidth;
  118.     int idealWidth;
  119.     fractpel hintValue;
  120.     char orientation;
  121.  
  122. /*
  123. By construction, width is never zero.  Therefore we can use the
  124. width value to determine if the hint has been rotated by a
  125. multiple of 90 degrees.
  126. */
  127.  
  128.     if (hP->width.y == 0)
  129.     {
  130.         orientation = 'v';    /* vertical */
  131.         IfTrace0((HintDebug > 0), "  vertical hint\n");
  132.     }
  133.     else if (hP->width.x == 0)
  134.     {
  135.         orientation = 'h';    /* horizontal */
  136.         IfTrace0((HintDebug > 0), "  horizontal hint\n");
  137.     }
  138.     else
  139.     {
  140.         IfTrace0((HintDebug > 0), "  hint not vertical or horizontal\n");
  141.         hintP->x = hintP->y = 0;
  142.         return;
  143.     }
  144.  
  145.     /* Compute currRef and currWidth with a unit of 1 pel */
  146.     if (orientation == 'v')    /* vertical */
  147.     {
  148.         currRef = hP->ref.x + currX;
  149.         currWidth = ABS(hP->width.x);
  150.     }
  151.     else if (orientation == 'h')    /* horizontal */
  152.     {
  153.         currRef = hP->ref.y + currY;
  154.         currWidth = ABS(hP->width.y);
  155.     }
  156.     else
  157.         /* error */
  158.     {
  159.         t1_abort("ComputeHint: invalid orientation");
  160.     }
  161.  
  162.     IfTrace4((HintDebug > 1),
  163.          "  currX=%p, currY=%p, currRef=%p, currWidth=%p\n",
  164.          currX, currY,
  165.          currRef, currWidth);
  166.  
  167.     if ((hP->hinttype == 'b')    /* Bar or stem */
  168.         || (hP->hinttype == 's'))    /* Serif */
  169.     {
  170.         idealWidth = NEARESTPEL(currWidth);
  171.         if (idealWidth == 0)
  172.             idealWidth = 1;
  173.         if (ODD(idealWidth))    /* Is ideal width odd? */
  174.         {
  175.             /* center "ref" over pel */
  176.             hintValue = FPFLOOR(currRef) + FPHALF - currRef;
  177.         }
  178.         else
  179.         {
  180.             /* align "ref" on pel boundary */
  181.             hintValue = FPROUND(currRef) - currRef;
  182.         }
  183.         if (HintDebug > 2)
  184.         {
  185.             IfTrace1(TRUE, "  idealWidth=%d, ", idealWidth);
  186.         }
  187.     }
  188.     else if (hP->hinttype == 'c')    /* Curve extrema */
  189.     {
  190.         /* align "ref" on pel boundary */
  191.         hintValue = FPROUND(currRef) - currRef;
  192.     }
  193.     else
  194.         /* error */
  195.     {
  196.         t1_abort("ComputeHint: invalid hinttype");
  197.     }
  198.  
  199.     IfTrace1((HintDebug > 1), "  hintValue=%p", hintValue);
  200.  
  201.     if (orientation == 'v')    /* vertical */
  202.     {
  203.         hintP->x = hintValue;
  204.         hintP->y = 0;
  205.     }
  206.     else if (orientation == 'h')    /* horizontal */
  207.     {
  208.         hintP->x = 0;
  209.         hintP->y = hintValue;
  210.     }
  211.     else
  212.         /* error */
  213.     {
  214.         t1_abort("ComputeHint: invalid orientation");
  215.     }
  216. }
  217.  
  218.  
  219. /*
  220.  * ProcessHint(hP, currX, currY, hintP) - Process a rasterization hint
  221.  */
  222. void t1_ProcessHint(struct hintsegment *hP, fractpel currX, fractpel currY, struct fractpoint *hintP)
  223. {
  224.     struct fractpoint thisHint;
  225.  
  226.     IfTrace4((HintDebug > 1), "  ref=(%p,%p), width=(%p,%p)",
  227.          hP->ref.x, hP->ref.y,
  228.          hP->width.x, hP->width.y);
  229.     IfTrace4((HintDebug > 1), ", %c %c %c %c",
  230.          hP->orientation, hP->hinttype,
  231.          hP->adjusttype, hP->direction);
  232.     IfTrace1((HintDebug > 1), ", label=%d\n", hP->label);
  233.  
  234.     if ((hP->adjusttype == 'm')    /* Move */
  235.         || (hP->adjusttype == 'a'))    /* Adjust */
  236.     {
  237.         /* Look up hint in oldHint table */
  238.         if ((hP->label >= 0) && (hP->label < MAXLABEL))
  239.         {
  240.             if (oldHint[hP->label].computed)
  241.                 /* Use old hint value if already computed */
  242.             {
  243.                 thisHint.x = oldHint[hP->label].hint.x;
  244.                 thisHint.y = oldHint[hP->label].hint.y;
  245.                 oldHint[hP->label].inuse = TRUE;
  246.             }
  247.             else
  248.                 /* Compute new value for hint and store it for future use */
  249.             {
  250.                 ComputeHint(hP, currX, currY, &thisHint);
  251.  
  252.                 oldHint[hP->label].hint.x = thisHint.x;
  253.                 oldHint[hP->label].hint.y = thisHint.y;
  254.                 oldHint[hP->label].inuse = TRUE;
  255.                 oldHint[hP->label].computed = TRUE;
  256.             }
  257.         }
  258.         else
  259.             /* error */
  260.         {
  261.             t1_abort("ProcessHint: invalid label");
  262.         }
  263.     }
  264.     else if (hP->adjusttype == 'r')    /* Reverse */
  265.     {
  266.         /* Use the inverse of the existing hint value to reverse hint */
  267.         if ((hP->label >= 0) && (hP->label < MAXLABEL))
  268.         {
  269.             if (oldHint[hP->label].inuse)
  270.             {
  271.                 thisHint.x = -oldHint[hP->label].hint.x;
  272.                 thisHint.y = -oldHint[hP->label].hint.y;
  273.                 oldHint[hP->label].inuse = FALSE;
  274.             }
  275.             else
  276.                 /* error */
  277.             {
  278.                 t1_abort("ProcessHint: label is not in use");
  279.             }
  280.         }
  281.         else
  282.             /* error */
  283.         {
  284.             t1_abort("ProcessHint: invalid label");
  285.         }
  286.  
  287.     }
  288.     else
  289.         /* error */
  290.     {
  291.         t1_abort("ProcessHint: invalid adjusttype");
  292.     }
  293.     IfTrace3((HintDebug > 1), "  label=%d, thisHint=(%p,%p)\n",
  294.          hP->label, thisHint.x, thisHint.y);
  295.  
  296.     hintP->x += thisHint.x;
  297.     hintP->y += thisHint.y;
  298.  
  299.     IfTrace2((HintDebug > 1), "  hint=(%p,%p)\n",
  300.          hintP->x, hintP->y);
  301. }
  302.  
  303.  
  304. /*
  305.  * id=subpath.Navigation Through Edge Lists
  306.  *
  307.  * For continuity checking purposes, we need to navigate through edge
  308.  * lists by the "subpath" chains and answer questions about edges.  The
  309.  * subpath chain links together edges that were part of the same subpath
  310.  * (no intervening move segments) when the interior of the path was
  311.  * calculated.  Here we use the term "edge" to mean every edge list
  312.  * that was created in between changes of direction.
  313.  *
  314.  * The subpath chains are singly-linked circular chains.  For the convenience
  315.  * of building them, they direction of the list (from edge to edge) is the
  316.  * reverse of the order in which they were built.  Within any single edge,
  317.  * the subpath chain goes from top-to-bottom.  (There might be a violation
  318.  * of this because of the way the user started the first chain; see
  319.  * :hdref refid=fixsubp..).
  320.  */
  321.  
  322. /*
  323.  * ISTOP() and ISBOTTOM() - Flag Bits for Edge Lists at the Top and
  324.  * Bottom of Their SubPaths
  325.  */
  326. #define   ISTOP(flag)     ((flag)&0x20)
  327. #define   ISBOTTOM(flag)  ((flag)&0x10)
  328.  
  329.  
  330. /*
  331.  * ISLEFT() - Flag Bit for Left Edges
  332.  */
  333. #define   ISLEFT(flag)    ((flag)&0x08)
  334.  
  335.  
  336. /*
  337.  * XofY() - Macro to Find X Value at Given Y
  338.  *
  339.  * This macro can only be used if it is known that the Y is within the
  340.  * given edgelist's ymin and ymax.
  341.  */
  342. #define   XofY(edge, y)   edge->xvalues[y - edge->ymin]
  343.  
  344.  
  345. /*
  346.  * findXofY() - Like XofY(), Except not Restricted
  347.  *
  348.  * If the Y is out of bounds of the given edgelist, this macro will
  349.  * call SearchXofY to search the edge's subpath chain for the correct
  350.  * Y range.  If the Y value is off the edge, MINPEL is returned.
  351.  */
  352. #define   findXofY(edge, y)  ((y < edge->ymin || y >= edge->ymax) ? SearchXofY(edge, y) : XofY(edge, y))
  353.  
  354.  
  355. /*
  356.  * SearchXofY() - Routine Called by FindXofY() for Difficult Cases
  357.  *
  358.  * The concept of this routine is to follow the subpath chain to find the
  359.  * edge just below (i.e., next in chain) or just above (i.e., immediately
  360.  * before in chain.  It is assumed that the Y value is no more than one
  361.  * off of the edge's range; XofY() could be replace by FindXofY() to
  362.  * call ourselves recursively if this were not true.
  363.  */
  364. static pel SearchXofY(
  365.         struct edgelist *edge,    /* represents edge */
  366.         pel y)                    /* 'y' value to find edge for */
  367. {
  368.     struct edgelist *e;            /* loop variable */
  369.  
  370.     if (y < edge->ymin)
  371.     {
  372.         if (ISTOP(edge->flag))
  373.             return (MINPEL);
  374.         for (e = edge->subpath; e->subpath != edge; e = e->subpath)
  375.         {;
  376.         }
  377.         if (e->ymax == edge->ymin)
  378.             return (XofY(e, y));
  379.     }
  380.     else if (y >= edge->ymax)
  381.     {
  382.         if (ISBOTTOM(edge->flag))
  383.             return (MINPEL);
  384.         e = edge->subpath;
  385.         if (e->ymin == edge->ymax)
  386.             return (XofY(e, y));
  387.     }
  388.     else
  389.         return (XofY(edge, y));
  390.  
  391.     t1_abort("bad subpath chain");
  392.     /*NOTREACHED*/
  393. }
  394.  
  395.  
  396. /*
  397.  * ISBREAK() Macro - Tests if an Edge List is at a "Break"
  398.  *
  399.  * The subpath chains are organized top to bottom.  When the bottom of
  400.  * a given edge is reached, the subpath chain points to the top of the
  401.  * next edge.  We call this a "break" in the chain.  The following macro
  402.  * is the simple test for the break condition:
  403.  */
  404. #define  ISBREAK(top,bot) (top->ymax != bot->ymin)
  405.  
  406.  
  407. /*
  408.  * ImpliedHorizontalLine() - Tests for Horizontal Connectivity
  409.  *
  410.  * This function returns true if two edges are connected horizontally.
  411.  * They are connected horizontally if they are consecutive in the subpath,
  412.  * and either we are at the bottom and the first edge is going down or we
  413.  * are at the top and the first edge is going up.
  414.  */
  415. #define  BLACKABOVE  -1
  416. #define  BLACKBELOW  +1
  417. #define  NONE         0
  418.  
  419. static int ImpliedHorizontalLine(
  420.         struct edgelist *e1,
  421.         struct edgelist *e2,    /* two edges to check */
  422.         int y)                    /* y where they might be connected */
  423. {
  424.     struct edgelist *e3, *e4;
  425.  
  426.     if (ISDOWN(e1->flag) == ISDOWN(e2->flag))
  427.         return (NONE);    /* can't be consecutive unless different directions */
  428. /*
  429. Now we check for consecutiveness:  Can we get from 'e1' to 'e2' with
  430. only one intervening break?  Can we get from 'e2' to 'e1' with only one
  431. intervening break?  'e3' will be as far as we can get after 'e1'; 'e4'
  432. will be has far as we can get after 'e2':
  433. */
  434.     for (e3 = e1; !ISBREAK(e3, e3->subpath); e3 = e3->subpath)
  435.     {;
  436.     }
  437.     for (e3 = e3->subpath; e3 != e2; e3 = e3->subpath)
  438.         if (ISBREAK(e3, e3->subpath))
  439.             break;
  440.  
  441.     for (e4 = e2; !ISBREAK(e4, e4->subpath); e4 = e4->subpath)
  442.     {;
  443.     }
  444.     for (e4 = e4->subpath; e4 != e1; e4 = e4->subpath)
  445.         if (ISBREAK(e4, e4->subpath))
  446.             break;
  447. /*
  448. If the edges are mutually consecutive, we must have horizontal lines
  449. both top and bottom:
  450. */
  451.     if (e3 == e2 && e4 == e1)
  452.         return (TRUE);
  453. /*
  454. If the edges are not consecutive either way, no horizontal lines are
  455. possible:
  456. */
  457.     if (e3 != e2 && e4 != e1)
  458.         return (NONE);
  459. /*
  460. Now let's swap 'e1' and 'e2' if necessary to enforce the rule that 'e2'
  461. follows 'e1'.  Remember that subpath chains go in the opposite direction
  462. from the way the subpaths were built; this led to the simplest way
  463. do build them.
  464. */
  465.     if (e4 != e1)
  466.     {
  467.         e2 = e1;
  468.         e1 = e3;    /* remember e3 == e2, this just swaps 'e1' and 'e2' */
  469.     }
  470. /*
  471. Now we have everything to return the answer:
  472. */
  473.     if (ISTOP(e1->flag) && y == e1->ymin)
  474.         return (ISDOWN(e2->flag));
  475.     else if (ISBOTTOM(e1->flag) && y == e1->ymax)
  476.         return (!ISDOWN(e2->flag));
  477.     else
  478.         t1_abort("ImpliedHorizontalLine:  why ask?");
  479.     /*NOTREACHED*/
  480. }
  481.  
  482. /*
  483.  * id=fixsubp.FixSubPaths() - Must be Called to Organize Subpath Chains
  484.  *
  485.  * The region-building code in Interior(), in particular splitedge(),
  486.  * maintains the rule that sub-paths are linked top-to-bottom except
  487.  * at breaks.  However, it is possible that there may be a "false break"
  488.  * because the user started the subpath in the middle of an edge (and
  489.  * went in the "wrong" direction from there, up instead of down).  This
  490.  * routine finds and fixes false breaks.
  491.  *
  492.  * Also, this routine sets the ISTOP and ISBOTTOM flags in the edge lists.
  493.  */
  494. static void FixSubPaths(
  495.         struct region *R)        /* anchor of region */
  496. {
  497.     struct edgelist *e;    /* fast loop variable                */
  498.     struct edgelist *edge;    /* current edge in region            */
  499.     struct edgelist *next;    /* next in subpath after 'edge'      */
  500.     struct edgelist *break1;    /* first break after 'next'        */
  501.     struct edgelist *break2;    /* last break before 'edge'        */
  502.     struct edgelist *prev;    /* previous edge for fixing links  */
  503.     int left = TRUE;
  504.  
  505.     for (edge = R->anchor; edge != NULL; edge = edge->link)
  506.     {
  507.  
  508.         if (left)
  509.             edge->flag |= ISLEFT(ON);
  510.         left = !left;
  511.  
  512.         next = edge->subpath;
  513.  
  514.         if (!ISBREAK(edge, next))
  515.             continue;
  516.         if (edge->ymax < next->ymin)
  517.             t1_abort("disjoint subpath?");
  518. /*
  519. 'edge' now contains an edgelist at the bottom of an edge, and 'next'
  520. contains the next subsequent edgelist in the subpath, which must be at
  521. the top.  We refer to this a "break" in the subpath.
  522. */
  523.         next->flag |= ISTOP(ON);
  524.         edge->flag |= ISBOTTOM(ON);
  525.  
  526.         if (ISDOWN(edge->flag) != ISDOWN(next->flag))
  527.             continue;
  528. /*
  529. We are now in the unusual case; both edges are going in the same
  530. direction so this must be a "false break" due to the way that the user
  531. created the path.  We'll have to fix it.
  532. */
  533.         for (break1 = next; !ISBREAK(break1, break1->subpath); break1 = break1->subpath)
  534.         {;
  535.         }
  536.  
  537.         for (e = break1->subpath; e != edge; e = e->subpath)
  538.             if (ISBREAK(e, e->subpath))
  539.                 break2 = e;
  540. /*
  541. Now we've set up 'break1' and 'break2'.  I've found the following
  542. diagram invaluable.  'break1' is the first break after 'next'.  'break2'
  543. is the LAST break before 'edge'.
  544. &drawing.
  545.          next
  546.         +------+     +---->+------+
  547.    +--->|    >-----+ |     |    >-----+
  548.    |    |      |   | |     |      |   |
  549.    | +-------------+ |  +-------------+
  550.    | |  |break1|     |  |  |      |
  551.    | +->|    >-------+  +->|    >-----+
  552.    |    |      |           |      |   |
  553.    |    |      |        +-------------+
  554.    |    +------+        |  |      |
  555.    | +----------------+ |  |      |
  556.    | |  +------+      | +->|    >-----+
  557.    | +->|    >-----+  |    |      |   |
  558.    |    |      |   |  | +-------------+
  559.    | +-------------+  | |  |      |
  560.    | |  |edge  |      | |  |break2|
  561.    | +->|    >-----+  | +->|    >-----+
  562.    |    |      |   |  |    |      |   |
  563.    |    |      |   |  |    |      |   |
  564.    |    |      |   |  |    |      |   |
  565.    |    +------+   |  |    +------+   |
  566.    |               |  |               |
  567.    +---------------+  +---------------+
  568.  
  569. &edrawing.
  570. We want to fix this situation by having 'edge' point to where 'break1'
  571. now points, and having 'break1' point to where 'break2' now points.
  572. Finally, 'break2' should point to 'next'.  Also, we observe that
  573. 'break1' can't be a bottom, and is also not a top unless it is the same
  574. as 'next':
  575. */
  576.         edge->subpath = break1->subpath;
  577.  
  578.         break1->subpath = break2->subpath;
  579.         if (ISBREAK(break1, break1->subpath))
  580.             t1_abort("unable to fix subpath break?");
  581.  
  582.         break2->subpath = next;
  583.  
  584.         break1->flag &= ~ISBOTTOM(ON);
  585.         if (break1 != next)
  586.             break1->flag &= ~ISTOP(ON);
  587.     }
  588. /*
  589. This region might contain "ambiguous" edges; edges exactly equal to
  590. edge->link.  Due to the random dynamics of where they get sorted into
  591. the list, they can yield false crossings, where the edges appear
  592. to cross.  This confuses our continuity logic no end.  Since we can
  593. swap them without changing the region, we do.
  594. */
  595.     for (edge = R->anchor, prev = NULL; VALIDEDGE(edge); prev = edge, edge = prev->link)
  596.     {
  597.  
  598.         if (!ISAMBIGUOUS(edge->flag))
  599.             continue;
  600.  
  601.         next = edge->subpath;
  602.  
  603.         while (ISAMBIGUOUS(next->flag) && next != edge)
  604.             next = next->subpath;
  605. /*
  606. We've finally found a non-ambiguous edge; we make sure it is left/right
  607. compatible with 'edge':
  608. */
  609.         if ((ISLEFT(edge->flag) == ISLEFT(next->flag) && ISDOWN(edge->flag) == ISDOWN(next->flag))
  610.             || (ISLEFT(edge->flag) != ISLEFT(next->flag) && ISDOWN(edge->flag) != ISDOWN(next->flag)))
  611.             continue;
  612.  
  613. /*
  614. Incompatible, we will swap 'edge' and the following edge in the list.
  615. You may think that there must be a next edge in this swath.  So did I.
  616. No!  If there is a totally ambiguous inner loop, for example, we could
  617. get all the way to the outside without resolving ambiguity.
  618. */
  619.         next = edge->link;    /* note new meaning of 'next' */
  620.         if (next == NULL || edge->ymin != next->ymin)
  621.             continue;
  622.         if (prev == NULL)
  623.             R->anchor = next;
  624.         else
  625.             prev->link = next;
  626.         edge->link = next->link;
  627.         next->link = edge;
  628.         edge->flag ^= ISLEFT(ON);
  629.         edge->flag &= ~ISAMBIGUOUS(ON);
  630.         next->flag ^= ISLEFT(ON);
  631.         next->flag &= ~ISAMBIGUOUS(ON);
  632.         edge = next;
  633.     }
  634. }
  635.  
  636.  
  637. /*
  638.  * DumpSubPaths()
  639.  *
  640.  * A debug tool.
  641.  */
  642. static void DumpSubPaths(struct edgelist *anchor)
  643. {
  644.     struct edgelist *edge, *e, *e2;
  645.     pel y;
  646.  
  647.     for (edge = anchor; VALIDEDGE(edge); edge = edge->link)
  648.     {
  649.         if (ISPERMANENT(edge->flag))
  650.             continue;
  651.         IfTrace0(TRUE, "BEGIN Subpath\n");
  652.         for (e2 = edge; !ISPERMANENT(e2->flag);)
  653.         {
  654.             if (ISDOWN(e2->flag))
  655.             {
  656.                 IfTrace1(TRUE, ". Downgoing edge's top at %x\n", e2);
  657.                 for (e = e2;; e = e->subpath)
  658.                 {
  659.                     IfTrace4(TRUE, ". . [%5d] %5d    @ %x[%x]\n",
  660.                       e->ymin, *e->xvalues, e, e->flag);
  661.                     for (y = e->ymin + 1; y < e->ymax; y++)
  662.                         IfTrace2(TRUE, ". . [%5d] %5d     \"\n", y, e->xvalues[y - e->ymin]);
  663.                     e->flag |= ISPERMANENT(ON);
  664.                     if (ISBREAK(e, e->subpath))
  665.                         break;
  666.                 }
  667.             }
  668.             else
  669.             {
  670.                 IfTrace1(TRUE, ". Upgoing edge's top at %x\n", e2);
  671.                 for (e = e2; !ISBREAK(e, e->subpath); e = e->subpath)
  672.                 {;
  673.                 }
  674.                 for (;; e = before(e))
  675.                 {
  676.                     IfTrace4(TRUE, ". . [%5d] %5d    @ %x[%x]\n",
  677.                          e->ymax - 1, e->xvalues[e->ymax - 1 - e->ymin], e, e->flag);
  678.                     for (y = e->ymax - 2; y >= e->ymin; y--)
  679.                         IfTrace2(TRUE, ". . [%5d] %5d      \"\n", y, e->xvalues[y - e->ymin]);
  680.                     e->flag |= ISPERMANENT(ON);
  681.                     if (e == e2)
  682.                         break;
  683.                 }
  684.             }
  685.             do
  686.             {
  687.                 e2 = before(e2);
  688.             }
  689.             while (!ISBREAK(before(e2), e2));
  690.         }
  691.     }
  692. }
  693.  
  694. static struct edgelist *before(struct edgelist *e)
  695. {
  696.     struct edgelist *r;
  697.  
  698.     for (r = e->subpath; r->subpath != e; r = r->subpath)
  699.     {;
  700.     }
  701.     return (r);
  702. }
  703.  
  704.  
  705. /*
  706.  * Fixing Region Continuity Problems
  707.  *
  708.  * Small regions may become disconnected when their connecting segments are
  709.  * less than a pel wide.  This may be correct in some applications, but in
  710.  * many (especially small font characters), it is more pleasing to keep
  711.  * connectivity.  ApplyContinuity() (invoked by +CONTINUITY on the
  712.  * Interior() fill rule) fixes connection breaks.  The resulting region
  713.  * is geometrically less accurate, but may be more pleasing to the eye.
  714.  */
  715.  
  716. /*
  717.  * Here are some macros which we will need:
  718.  */
  719. #define IsValidPel(j) (j!=MINPEL)
  720.  
  721.  
  722. /*
  723.  * writeXofY() - Stuffs an X Value Into an "edgelist"
  724.  *
  725.  * writeXofY writes an x value into an edge at position 'y'.  It must
  726.  * update the edge's xmin and xmax.  If there is a possibility that this
  727.  * new x might exceed the region's bounds, updating those are the
  728.  * responsibility of the caller.
  729.  */
  730. static void writeXofY(
  731.         struct edgelist *e,        /* relevant edgelist */
  732.         int y,                    /* y value */
  733.         int x)                    /* new x value */
  734. {
  735.     if (e->xmin > x)
  736.         e->xmin = x;
  737.     if (e->xmax < x)
  738.         e->xmax = x;
  739.     e->xvalues[y - e->ymin] = x;
  740. }
  741.  
  742.  
  743. /*-------------------------------------------------------------------------*/
  744. /* the following three macros tell us whether we are at a birth point, a    */
  745. /* death point, or simply in the middle of the character                */
  746. /*-------------------------------------------------------------------------*/
  747. #define WeAreAtTop(e,i) (ISTOP(e->flag) && e->ymin == i)
  748. #define WeAreAtBottom(e,i) (ISBOTTOM(e->flag) && e->ymax-1 == i)
  749. #define WeAreInMiddle(e,i) \
  750.       ((!ISTOP(e->flag) && !ISBOTTOM(e->flag))||(i < e->ymax-1 && i > e->ymin))
  751.  
  752.  
  753. /*
  754.  * The following macro tests if two "edgelist" structures are in the same
  755.  * swath:
  756.  */
  757. #define SAMESWATH(e1,e2)  (e1->ymin == e2->ymin)
  758.  
  759.  
  760. /*
  761.  * CollapseWhiteRun() - Subroutine of ApplyContinuity()
  762.  *
  763.  * When we have a white run with an implied horizontal line above or
  764.  * below it, we better have black on the other side of this line.  This
  765.  * function both tests to see if black is there, and adjusts the end
  766.  * points (collapses) the white run as necessary if it is not.  The
  767.  * goal is to collapse the white run as little as possible.
  768.  */
  769. static void CollapseWhiteRun(
  770.         struct edgelist *anchor,/* anchor of edge list */
  771.         pel yblack,                /* y of (hopefully) black run above or below */
  772.         struct edgelist *left,     /* edgelist at left of WHITE run */
  773.         struct edgelist *right, /* edgelist at right of WHITE run */
  774.         pel ywhite)                /* y location of white run */
  775. {
  776.     struct edgelist *edge;
  777.     struct edgelist *swathstart = anchor;
  778.     pel x;
  779.  
  780.     if (XofY(left, ywhite) >= XofY(right, ywhite))
  781.         return;
  782. /*
  783. Find the swath with 'yblack'.  If we don't find it, completely collapse
  784. the white run and return:
  785. */
  786.     while (VALIDEDGE(swathstart))
  787.     {
  788.         if (yblack < swathstart->ymin)
  789.         {
  790.             writeXofY(left, ywhite, XofY(right, ywhite));
  791.             return;
  792.         }
  793.         if (yblack < swathstart->ymax)
  794.             break;
  795.         swathstart = swathstart->link->link;
  796.     }
  797.     if (!VALIDEDGE(swathstart))
  798.     {
  799.         writeXofY(left, ywhite, XofY(right, ywhite));
  800.         return;
  801.     }
  802. /*
  803. Now we are in the swath that contains 'y', the reference line above
  804. or below that we are trying to maintain continuity with.  If black
  805. in this line begins in the middle of our white run, we must collapse
  806. the white run from the left to that point.  If black ends in the
  807. middle of our white run, we must collapse the white run from the right
  808. to that point.
  809. */
  810.     for (edge = swathstart; VALIDEDGE(edge); edge = edge->link)
  811.     {
  812.  
  813.         if (!SAMESWATH(swathstart, edge))
  814.             break;
  815.         if (XofY(edge, yblack) > XofY(left, ywhite))
  816.         {
  817.             if (ISLEFT(edge->flag))
  818.             {
  819.                 x = XofY(edge, yblack);
  820.                 if (XofY(right, ywhite) < x)
  821.                     x = XofY(right, ywhite);
  822.                 writeXofY(left, ywhite, x);
  823.             }
  824.             else
  825.             {
  826.                 x = XofY(edge, yblack);
  827.                 while (edge->link != NULL && SAMESWATH(edge, edge->link)
  828.                        && x >= XofY(edge->link, yblack))
  829.                 {
  830.                     edge = edge->link->link;
  831.                     x = XofY(edge, yblack);
  832.                 }
  833.                 if (x < XofY(right, ywhite))
  834.                     writeXofY(right, ywhite, x);
  835.                 return;
  836.             }
  837.         }
  838.     }
  839.     writeXofY(left, ywhite, XofY(right, ywhite));
  840. }
  841.  
  842.  
  843. /*
  844.  * ApplyContinuity() - Fix False Breaks in a Region
  845.  *
  846.  * This is the externally visible routine called from the REGIONS module
  847.  * when the +CONTINUITY flag is on the Interior() fill rule.
  848.  */
  849. void ApplyContinuity(struct region *R)
  850. {
  851.     struct edgelist *left;
  852.     struct edgelist *right;
  853.     struct edgelist *edge, *e2;
  854.     pel rightXabove, rightXbelow, leftXabove, leftXbelow;
  855.     pel leftX, rightX;
  856.     int i;
  857.     long newcenter, abovecenter, belowcenter;
  858.  
  859.     FixSubPaths(R);
  860.     if (RegionDebug >= 3)
  861.         DumpSubPaths(R->anchor);
  862.     left = R->anchor;
  863. /* loop through and do all of the easy checking. ( no tops or bottoms) */
  864.     while (VALIDEDGE(left))
  865.     {
  866.         right = left->link;
  867.         for (i = left->ymin; i < left->ymax; ++i)
  868.         {
  869.             leftX = findXofY(left, i);
  870.             rightX = findXofY(right, i);
  871.             leftXbelow = findXofY(left, i + 1);
  872.             rightXbelow = findXofY(right, i + 1);
  873.             if (rightX <= leftX)
  874.             {
  875. /* then, we have a break in a near vertical line */
  876.                 leftXabove = findXofY(left, i - 1);
  877.                 rightXabove = findXofY(right, i - 1);
  878.                 if (IsValidPel(leftXabove) && IsValidPel(rightXabove))
  879.                 {
  880.                     abovecenter = leftXabove + rightXabove;
  881.                 }
  882.                 else
  883.                 {
  884.                     abovecenter = leftX + rightX;
  885.                 }
  886.                 if (IsValidPel(leftXbelow) && IsValidPel(rightXbelow))
  887.                 {
  888.                     belowcenter = leftXbelow + rightXbelow;
  889.                 }
  890.                 else
  891.                 {
  892.                     belowcenter = leftX + rightX;
  893.                 }
  894.                 newcenter = abovecenter + belowcenter;
  895.                 if (newcenter > 4 * leftX)
  896.                 {
  897.                     rightX = rightX + 1;
  898.                 }
  899.                 else if (newcenter < 4 * leftX)
  900.                 {
  901.                     leftX = leftX - 1;
  902.                 }
  903.                 else
  904.                 {
  905.                     rightX = rightX + 1;
  906.                 }
  907.                 writeXofY(right, i, rightX);
  908.                 writeXofY(left, i, leftX);
  909.                 if (rightX > R->xmax)
  910.                 {
  911.                     R->xmax = rightX;
  912.                 }
  913.                 if (leftX < R->xmin)
  914.                 {
  915.                     R->xmin = leftX;
  916.                 }
  917.             }
  918.             if (!WeAreAtBottom(left, i) && (leftXbelow >= rightX))
  919.             {
  920. /* then we have a break in a near horizontal line in the middle */
  921.                 writeXofY(right, i, leftXbelow);
  922.             }
  923.             if (!WeAreAtBottom(right, i) && (leftX >= rightXbelow))
  924.             {
  925. /* then we have a break in a near horizontal line in the middle */
  926.                 writeXofY(left, i, rightXbelow);
  927.             }
  928.         }
  929.         left = right->link;
  930.     }
  931. /*
  932. There may be "implied horizontal lines" between edges that have
  933. implications for continuity.  This loop looks for white runs that
  934. have implied horizontal lines on the top or bottom, and calls
  935. CollapseWhiteRuns to check and fix any continuity problems from
  936. them.
  937. */
  938.     for (edge = R->anchor; VALIDEDGE(edge); edge = edge->link)
  939.     {
  940.         if ((!ISTOP(edge->flag) && !ISBOTTOM(edge->flag)) || ISLEFT(edge->flag))
  941.             continue;    /* at some future date we may want left edge logic here too */
  942.         for (e2 = edge->link; VALIDEDGE(e2) && SAMESWATH(edge, e2); e2 = e2->link)
  943.         {
  944.             if (ISTOP(e2->flag) && ISTOP(edge->flag)
  945.                 && NONE != ImpliedHorizontalLine(edge, e2, edge->ymin))
  946.             {
  947.                 if (ISLEFT(e2->flag))
  948.                     CollapseWhiteRun(R->anchor, edge->ymin - 1,
  949.                               edge, e2, edge->ymin);
  950.             }
  951.             if (ISBOTTOM(e2->flag) && ISBOTTOM(edge->flag)
  952.                 && NONE != ImpliedHorizontalLine(edge, e2, edge->ymax))
  953.             {
  954.                 if (ISLEFT(e2->flag))
  955.                     CollapseWhiteRun(R->anchor, edge->ymax,
  956.                           edge, e2, edge->ymax - 1);
  957.             }
  958.         }
  959.     }
  960. }
  961.